﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using EHR.Common;
using System.Windows.Browser;
using EHR.Views;
using CommunitySystem;
using System.ComponentModel;
using CommunitySystem.Common;

namespace CommunitySystem
{
    public partial class App : Application
    {
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;          
            this.UnhandledException += this.Application_UnhandledException;          
            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            InitialDictionary init = new InitialDictionary();
            init.Init();

            #region 密码狗操作
            HtmlDocument doc = HtmlPage.Document;
            var ui = doc.GetElementById("sign");
            dog = new PassWordDog();
            Session.dog = dog;
            HtmlPage.RegisterScriptableObject("dog", dog);
            //HtmlPage.Window.Invoke("Checking", null);
            #endregion
            string uri = HtmlPage.Document.DocumentUri.ToString();
            string[] paras = uri.Split('?');
            if (paras.Length >= 2)
            {
                Session.QueryString = paras[1];
                this.RootVisual = new MainPage();
            }
            else
            {
                //LoginFromOA oa = new LoginFromOA();
                //oa.NeedFromOA();
                //this.RootVisual = oa;
                this.RootVisual = new MainPage();
            }
        }
        private  IsolatedStorageSettings appSettings = null;
        private void Application_Exit(object sender, EventArgs e)
        {
            if (appSettings == null)
                appSettings = IsolatedStorageSettings.ApplicationSettings;
            if (appSettings.Contains(Session.LOGINUSER))
                appSettings.Remove(Session.LOGINUSER);
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            //Worker.IsBusy = false;
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }

        public PassWordDog dog = null;

        public static CommunitySystem.Assert.FlipSwitch _FlipSwitch = null;

        /// <summary>
        /// 当前模块的TabControl
        /// </summary>
        public static TabControl currentTabControl = null;
        private void close_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                string tabname = btn.Tag.ToString();
                if (currentTabControl != null)
                {
                    var item = (TabItem)currentTabControl.Items.Where(c => (c as TabItem).Name.Equals(tabname)).FirstOrDefault();
                    if (currentTabControl.Items.Count > 1)
                        currentTabControl.Items.Remove(item);
                }
                //var item = (TabItem)tabControl1.Items.Where(c => (c as TabItem).Name.Equals(tabname)).FirstOrDefault();
                //tabControl1.Items.Remove(item);
            }
        }

      
    }

    public class PassWordDog : INotifyPropertyChanged
    {
        private string _Password = "";
        [ScriptableMember()]
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        private bool _HasDow;
        [ScriptableMember()]
        public bool HasDog
        {
            get { return _HasDow; }
            set
            {
                if (value != _HasDow)
                {
                    _HasDow = value;                   
                    OnPropertyChanged("HasDog");
                }
            }
        }

        private string _DateString;
        [ScriptableMember()]
        public string DateString
        {
            get { return _DateString; }
            set { _DateString = value; }
        }
        private string  _CertBook;
        [ScriptableMember()]
        public string  CertBook
        {
            get { return _CertBook; }
            set { _CertBook = value; }
        }
        


        public event PropertyChangedEventHandler PropertyChanged;
    }
}
